package com.scau.ums.controller;

import com.scau.ums.model.Maintainer;
import com.scau.ums.model.User;
import com.scau.ums.service.imp.info.InfoImp;
import com.scau.ums.service.imp.login.LoginImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * author: 莫伟杰
 * date： 2019/7/18 10:41
 */

@Controller
public class LoginController {

    //自动注入的AdminLoginImp对象
    @Autowired
    private LoginImp login;

    @Autowired
    private InfoImp infoImp;

    //接受"/login"的get请求，显示登录页面"/jsp/login.jsp"
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String toLogin(HttpServletRequest request)
    {
        if(request.getSession().getAttribute("USER_ID")==null)
            return "login";
        else
            return "redirect:/";
    }

    //接受"/login"的post请求，登录主功能
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userID = request.getParameter("id");
        String password = request.getParameter("pwd");
        String choice= request.getParameter("choice");
        int type;
        switch (choice){
            case "student":type=User.STUDENT;break;
            case "teacher":type=User.TEACHER;break;
            case "maintain":type=User.MAINTAINER;break;
            case "admin":type=User.ADMIN;break;
            default:type=User.STUDENT;
        }
        User user = login.login(userID,password,type);
        if(user!=null)
        {
            request.getSession().setAttribute("USER_ID",user.getId());
            request.getSession().setAttribute("USER_NAME",user.getName());
            request.getSession().setAttribute("USER_TYPE",user.getUserType());
            if(user.getUserType()==User.ADMIN) return "redirect:adminEntrance";
            if(user.getUserType()==User.MAINTAINER)
            {
                Maintainer maintainer = (Maintainer)user;
                request.getSession().setAttribute("MAINTAINER_ROLE",maintainer.getRole());
                return "redirect:maintainerEntrance";
            }
            return "redirect:/";
        }
        else
        {
            request.setAttribute("msg","登录失败");
            return "forward:/WEB-INF/jsp/login.jsp";
        }
    }

    //接受"/logout"，注销功能
    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request)
    {
        //清除session记录
        request.getSession().invalidate();
        System.out.println("退出登录成功！");
        return "logout";
    }

    @RequestMapping("/countMessage")
    @ResponseBody
    public int countMessage(HttpServletRequest request){
        String id=(String)request.getSession().getAttribute("USER_ID");
        int unCheckedInfo=infoImp.countUncheckedInfo(id);
//        System.out.println("========未读信息有"+unCheckedInfo);
        return unCheckedInfo;
    }

}
